<?php

	//get the variables
	$class = $_GET["class"];
	$professor = $_GET["professor"];
	$fileName = $_GET["fileName"];
	
	//get the ids
	$classId = checkClass($class);
	$professorId = checkProfessor($professor);
	$privacy = getPrivacy("Public");
	
	//generate the query
	$query = "SELECT * FROM CS275.files WHERE privacy='$privacy'";
	if(isset($_GET["fileName"])){ //if we have the filename proprty
		$query = $query . " AND name LIKE '%{$fileName}%'";
	}
	if(isset($_GET["class"]) && $class != "None"){ //if we have the class property
		$query = $query . " AND course='$classId'";
	}
	if(isset($_GET["professor"]) && $professor != "None"){ //if we have the professor property
		$query = $query . " AND professor='$professorId'";
	}
	
	//connect to database
	$link = mysql_connect('localhost', 'root', 'admin');
	if(!$link){
		die('Could not connect: ' . mysql_error());	
	}
	
	//perform query
	$result = mysql_query($query, $link);
	
	$stack = array();
	//if we get results
	while($row = mysql_fetch_assoc($result)){
		array_push($stack, array("id" => $row["id"], "name" => $row["name"]));
	}
	
	//free results
	mysql_free_result($result);
	
	//close link
	mysql_close($link);
	
	//output json
	echo(json_encode(array("results" => $stack)));
	
	function checkProfessor($prof){
		$query = "SELECT * FROM CS275.professor WHERE professor='$prof'";
		
		//establish connection to MySQL
		$link = mysql_connect('localhost', 'root', 'admin');
		if(!$link){
			$successful = false;
			die('Could not connect: ' . mysql_error());	
		}
		
		//perform query
		$result = mysql_query($query, $link);
		
		$id = -1;
		//if we get results
		while($row = mysql_fetch_assoc($result)){
			$id = $row["id"];
		}
		
		//free results
		mysql_free_result($result);
		
		//close link
		mysql_close($link);
		
		//output json
		return $id;
	}
	
	function getPrivacy($privacy){
		$query = "SELECT * FROM CS275.privacy WHERE privacy='$privacy'";
		
		//establish connection to MySQL
		$link = mysql_connect('localhost', 'root', 'admin');
		if(!$link){
			$successful = false;
			die('Could not connect: ' . mysql_error());	
		}
		
		//perform query
		$result = mysql_query($query, $link);
		
		$id = 1;
		//if we get results
		while($row = mysql_fetch_assoc($result)){
			$id = $row["id"];
		}
		
		//free results
		mysql_free_result($result);
		
		//close link
		mysql_close($link);
		
		//output json
		return $id;
	}
	
	function checkClass($class){
		$query = "SELECT * FROM CS275.class WHERE course='$class'";
		
		//establish connection to MySQL
		$link = mysql_connect('localhost', 'root', 'admin');
		if(!$link){
			$successful = false;
			die('Could not connect: ' . mysql_error());	
		}
		
		//perform query
		$result = mysql_query($query, $link);
		
		$id = -1;
		//if we get results
		while($row = mysql_fetch_assoc($result)){
			$id = $row["id"];
		}
		
		//free results
		mysql_free_result($result);
		
		//close link
		mysql_close($link);
		
		//output json
		return $id;
	}

?>